{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true,
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "[![AWS SDK for pandas](_static/logo.png \"AWS SDK for pandas\")](https://github.com/aws/aws-sdk-pandas)\n",
    "\n",
    "# 28 - Amazon DynamoDB"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "## Writing Data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "from datetime import datetime\n",
    "from decimal import Decimal\n",
    "from pathlib import Path\n",
    "\n",
    "import pandas as pd\n",
    "from boto3.dynamodb.conditions import Attr, Key\n",
    "\n",
    "import awswrangler as wr"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "### Writing DataFrame"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "table_name = \"movies\"\n",
    "\n",
    "df = pd.DataFrame(\n",
    "    {\n",
    "        \"title\": [\"Titanic\", \"Snatch\", \"The Godfather\"],\n",
    "        \"year\": [1997, 2000, 1972],\n",
    "        \"genre\": [\"drama\", \"caper story\", \"crime\"],\n",
    "    }\n",
    ")\n",
    "wr.dynamodb.put_df(df=df, table_name=table_name)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "### Writing CSV file"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "filepath = Path(\"items.csv\")\n",
    "df.to_csv(filepath, index=False)\n",
    "wr.dynamodb.put_csv(path=filepath, table_name=table_name)\n",
    "filepath.unlink()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "### Writing JSON files"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "filepath = Path(\"items.json\")\n",
    "df.to_json(filepath, orient=\"records\")\n",
    "wr.dynamodb.put_json(path=\"items.json\", table_name=table_name)\n",
    "filepath.unlink()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "### Writing list of items"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "items = df.to_dict(orient=\"records\")\n",
    "wr.dynamodb.put_items(items=items, table_name=table_name)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Reading Data"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Read Items"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Limit Read to 5 items\n",
    "wr.dynamodb.read_items(table_name=table_name, max_items_evaluated=5)\n",
    "\n",
    "# Limit Read to Key expression\n",
    "wr.dynamodb.read_items(\n",
    "    table_name=table_name, key_condition_expression=(Key(\"title\").eq(\"Snatch\") & Key(\"year\").eq(2000))\n",
    ")"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Read PartiQL"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>year</th>\n",
       "      <th>genre</th>\n",
       "      <th>title</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2000</td>\n",
       "      <td>caper story</td>\n",
       "      <td>Snatch</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   year        genre   title\n",
       "0  2000  caper story  Snatch"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "wr.dynamodb.read_partiql_query(\n",
    "    query=f\"SELECT * FROM {table_name} WHERE title=? AND year=?\",\n",
    "    parameters=[\"Snatch\", 2000],\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Executing statements"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[]"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "title = \"The Lord of the Rings: The Fellowship of the Ring\"\n",
    "year = datetime.now().year\n",
    "genre = \"epic\"\n",
    "rating = Decimal(\"9.9\")\n",
    "plot = \"The fate of Middle-earth hangs in the balance as Frodo and eight companions begin their journey to Mount Doom in the land of Mordor.\"\n",
    "\n",
    "# Insert items\n",
    "wr.dynamodb.execute_statement(\n",
    "    statement=f\"INSERT INTO {table_name} VALUE {{'title': ?, 'year': ?, 'genre': ?, 'info': ?}}\",\n",
    "    parameters=[title, year, genre, {\"plot\": plot, \"rating\": rating}],\n",
    ")\n",
    "\n",
    "# Select items\n",
    "wr.dynamodb.execute_statement(\n",
    "    statement=f'SELECT * FROM \"{table_name}\" WHERE title=? AND year=?',\n",
    "    parameters=[title, year],\n",
    ")\n",
    "\n",
    "# Update items\n",
    "wr.dynamodb.execute_statement(\n",
    "    statement=f'UPDATE \"{table_name}\" SET info.rating=? WHERE title=? AND year=?',\n",
    "    parameters=[Decimal(10), title, year],\n",
    ")\n",
    "\n",
    "# Delete items\n",
    "wr.dynamodb.execute_statement(\n",
    "    statement=f'DELETE FROM \"{table_name}\" WHERE title=? AND year=?',\n",
    "    parameters=[title, year],\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "## Deleting items"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "wr.dynamodb.delete_items(items=items, table_name=\"table\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.8.5 ('awswrangler-mo8sEp3D-py3.8')",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  },
  "vscode": {
   "interpreter": {
    "hash": "350d68fa765a50d15f89103ff6102f3b96ae3e7bdc6e5e7a4956e4c1d21b94bd"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
